package com.kly.interview.blackip;

import cn.hutool.bloomfilter.BitMapBloomFilter;
import org.yaml.snakeyaml.Yaml;

import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @date 2025/5/4
 */
public class BlackIpUtil {

    private static BitMapBloomFilter bitMapBloomFilter;


    /**
     * 判断是否为黑名单ip
     */
    public static boolean isBlackIp(String ip) {
        return bitMapBloomFilter.contains(ip);
    }

    /**
     * 重建黑名单ip
     */
    public static void rebuildBlackIp(String config) {
        bitMapBloomFilter = new BitMapBloomFilter(1000);
        Yaml yaml = new Yaml();
        Map map = yaml.loadAs(config, Map.class);
        List<String> ips = (List<String>) map.get("black-ip");
        synchronized (BlackIpUtil.class) {
            for (String ip : ips) {
                bitMapBloomFilter.add(ip);
            }
        }
    }

}
